/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Entities.Nivelusuario;
import Entities.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ADMIN
 */
@WebServlet(name = "nuevoUsuario", urlPatterns = {"/nuevoUsuario"})
public class nuevoUsuario extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
               String nombre=String.valueOf(request.getParameter("txtnombre"));
               String apellido=String.valueOf(request.getParameter("txtapellido"));
               String usuario=String.valueOf(request.getParameter("txtusuario"));
               String pass=String.valueOf(request.getParameter("txtpass"));
               int nuid=Integer.parseInt(String.valueOf(request.getParameter("cbnivel")));
               
               EntityManagerFactory emf=Persistence.createEntityManagerFactory("PU");
               EntityManager em=emf.createEntityManager();
               
               em.getTransaction().begin();
               Usuario us=new Usuario();
               //us.setIdusuario();
               us.setNombre(nombre);
               us.setApellido(apellido);
               us.setUsuario(usuario);
               us.setContrasena(pass);
               us.setIdnivel(nuid);
               us.setIdestado(1);

               
               em.persist(us); 
               em.getTransaction().commit();
               
               
               out.println("CREACION EXITOSA!!");
               
               
               
               em=emf.createEntityManager();
               Query result = em.createNamedQuery("Nivelusuario.findByIdnivel");
               result.setParameter("idnivel", nuid);
               String nivel=null;
               
               java.util.List empleadosList = result.getResultList();
               Iterator itr=empleadosList.iterator();
               
               while(itr.hasNext())
                {
                    Nivelusuario nu=(Nivelusuario)itr.next();
                    nivel=nu.getNivel();
                }
               
               out.println("<table width='288' border='0'>");
               out.println("<tr>");
               out.println("<td colspan='2'>USUARIO CREADO</td>");
               out.println("</tr>");
               out.println("<tr>");
               out.println("<td width='92'>Nombre:</td>");
               out.println("<td>"+nombre+"</td>");
                
               out.println("</tr>");
               out.println("<tr>");
               out.println("<td>Apellido:</td>");
               out.println("<td>"+apellido+"</td>");
               out.println("</tr>");
               out.println("<tr>");
               out.println("<td>Usuario:</td>");
               out.println("<td>"+usuario+"</td>");
               out.println("</tr>");
               out.println("<tr>");
               out.println("<td>Contraseña:</td>");
               out.println("<td>"+pass+"</td>");
               out.println("</tr>");
               out.println("<tr>");
               out.println("<td>Nivel</td>");
               
               out.println("<td>"+nivel+"</td>");
                
               out.println("</tr>");
            out.println("</table>");
               em.close();
               emf.close();
               
               
               
               
        } catch(Exception e) {            
         System.err.print(e.toString());   
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
